package org.jxkj.app

import java.io.IOException
import java.text.SimpleDateFormat
import java.util._

import com.ybsoftware.ApiEntry
import org.apache.spark.sql.SparkSession
import org.jxkj.client.{NodeJsClient, PmuClient}
import org.jxkj.config.MysqlConfig
import org.jxkj.data.edos.EdosApiImpl
import org.jxkj.data.hbase.HBaseDaoWideImpl
import org.jxkj.utils.DruidUtils
import org.slf4j.{Logger, LoggerFactory}

object Application {
  val log: Logger = LoggerFactory.getLogger(Application.getClass)
  /**
   * 初始化spark需要的连接数据库参数
   */
  def initEnv(): Properties = {
    try {
      val is = ClassLoader.getSystemResourceAsStream("env.properties")
      val prop = new Properties
      prop.load(is)
      MysqlDml.url = prop.getProperty("edos.url")
      MysqlDml.url2 = prop.getProperty("hmsdb.url")
      MysqlDml.user = prop.getProperty("username")
      MysqlDml.user2 = prop.getProperty("username")
      MysqlDml.password = prop.getProperty("password")
      MysqlDml.password2 = prop.getProperty("password")
      MysqlDml.fileprefix = prop.getProperty("csv.filesrv.prefix")
      MysqlConfig.url = prop.getProperty("edos.url")
      MysqlConfig.url2 = prop.getProperty("hmsdb.url")
      MysqlConfig.user = prop.getProperty("username")
      MysqlConfig.user2 = prop.getProperty("username")
      MysqlConfig.password = prop.getProperty("password")
      MysqlConfig.password2 = prop.getProperty("password")
      MysqlConfig.fileprefix = prop.getProperty("csv.filesrv.prefix")
      log.info("加载数据库配置OK...")
      // 加载nodejs配置
      NodeJsClient.IP = prop.getProperty("nodejs.server.ip")
      NodeJsClient.R_PORT = Integer.valueOf(prop.getProperty("nodejs.server.read.port"))
      NodeJsClient.W_PORT = Integer.valueOf(prop.getProperty("nodejs.server.write.port"))
      // 加载PMU配置
      PmuClient.IP = prop.getProperty("pmu.server.ip")
      PmuClient.R_PORT = Integer.valueOf(prop.getProperty("pmu.server.read.port"))
      prop
    } catch {
      case e: IOException =>
        log.error("加载数据库配置出现错误：" + e.getMessage)
        throw e
    }
  }

  /**
   * 根据配置文件初始化spark运行模式
   * @param mode
   * @return
   */
  def initSpark(mode:String):SparkSession={
    val ss = if(mode.equals("local")) {
      SparkSession
      .builder()
      .appName("mySpark")
      .master("local[*]")
      .getOrCreate()
    }else{
      //==============================spark集群运行===============================================
        SparkSession
          .builder()
          .appName("mySpark ")
          // .master("local[*]")
          // .master(("spark://jx01:8070"))
          .config("spark.default.parallelism", 200)
          .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
          .config("spark.sql.crossJoin.enabled","true")
          .config("spark.shuffle.consolidateFiles","true")
          .config("spark.shuffle.file.buffer","128k")
          .config("spark.sql.warehouse.dir", "hdfs://hms01:8020/warehouse/tablespace/managed/hive")
          .config("hive.exec.dynamic.partition", "true")
          .config("hive.exec.dynamic.partition.mode", "nonstrict")
          .config("spark.debug.maxToStringFields", "500")
          // .config("spark.executor.memory", "6g")
          // .config("spark.executor.cores", "3")
          // .config("spark.driver.memory", "4g")
          .enableHiveSupport()
          .getOrCreate()
    }
    ss
  }

  def main(args: Array[String]): Unit = {
    val prop=initEnv()
    val ss = initSpark(prop.getProperty("spark.mode"))
    val sc = ss.sparkContext
    sc.setLogLevel("INFO")
    log.info("Spark运行模式:{}", sc.master)
    //初始化数据服务
    val provider=prop.getProperty("daoprovider","edos")
    val hTable = if (provider.equals("edos")) {
      //初始化edos
      val edosip=prop.getProperty("edos.ip")
      val edosport:Int=Integer.valueOf(prop.getProperty("edos.port"))
      val edosapi = new ApiEntry(edosip,edosport)
      new EdosApiImpl(edosapi)
    }else {new HBaseDaoWideImpl}
    log.info("daoprovider={}",hTable)
    hTable.setWebserviceUrl(prop.getProperty("webservice.url"))
    // ==================================================================================
    import ss.implicits._

    //读mysql
    val pointDF = ss.read
      .format("jdbc")
      .option("url", MysqlDml.url2)
      .option("dbtable", "hms_cm_point")
      .option("user", MysqlDml.user2)
      .option("password", MysqlDml.password2)
      .option("driver", MysqlDml.driver2)
      .load()
    pointDF.createOrReplaceTempView("hms_cm_point")
    val scopeDF = ss.sql("select point_code,scope_h,scope_l from hms_cm_point where scope_h is not null and scope_l is not null").dropDuplicates(Seq("point_code"))
    scopeDF.createOrReplaceTempView("hms_cm_point2")
    // val strings = Array("JKXT_EJ_AI03092","JKXT_EJ_AI03087")
    //val arrayString: Array[String] = confDF.map{ x=>x.getString(0)}.collect()
    //val arrayString2: Array[String] = confDF.filter($"report_desc".notEqual("")).map{ x=>x.getString(0)}.collect()
    val step = args(2).toInt //时间间隔
    val time = Array(args(0), args(1))
    val time2 = Array(args(0), args(1))

    val sdfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

    //变损率及厂用电
    if (time(0) <= time(1) && (args(3) == "3" || args(3) == "all")) {
      log.info("TrfLoss begining")
      //TrfLoss.calculateData_mth(ss,sc,hTable,time3,step)
      var opt_start_time = sdfs.format(new Date)
      //MysqlDml.insert("TrfLoss",opt_start_time,opt_start_time,"begin",time3(0),time3(1))
      try {
        TrfLoss.calcTransformLoss(ss, sc, hTable, time, step)

        var opt_end_time = sdfs.format(new Date)
        MysqlDml.insert2("TrfLoss", "", "", "变损率及厂用电", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","变损率及厂用电")
      } catch {
        case ex: Exception => MysqlDml.insert2("TrfLoss", "", "", "变损率及厂用电", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
          ex.printStackTrace()
      }

    }

    //机组开限及最大出力
    if (time(0) <= time(1) && (args(3) == "4" || args(3) == "all2")) {
      log.info("UnitPower begining")
      var opt_start_time = sdfs.format(new Date)
      //MysqlDml.insert("UnitPower",opt_start_time,opt_start_time,"begin",time(0),time(1))
      try {
        UnitPower.calcUnitPower(ss, sc, hTable, time, step)
        var opt_end_time = sdfs.format(new Date)
        //MysqlDml.insert("UnitPower",opt_start_time,opt_end_time,"end",time(0),time(1))
        MysqlDml.insert2("UnitPower", "", "", "机组开限及最大出力", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","机组开限及最大出力")
      } catch {
        case ex: Exception => MysqlDml.insert2("UnitPower", "", "", "机组开限及最大出力", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
          ex.printStackTrace()
      }
    }

    //换算油位和共事故油罐
    if (time(0) <= time(1) && (args(3) == "5" || args(3) == "all2")) {
      log.info("OilLevelConvert begining")
      var opt_start_time = sdfs.format(new Date)
      //MysqlDml.insert("OilLevelConvert",opt_start_time,opt_start_time,"begin",time(0),time(1))
      try {
        OilLevelConvert.calcOilLevelConvert(ss, sc, hTable, time, step)
        var opt_end_time = sdfs.format(new Date)
        //MysqlDml.insert("OilLevelConvert",opt_start_time,opt_end_time,"end",time(0),time(1))
        MysqlDml.insert2("OilLevelConvert", "", "", "换算油位和共事故油罐", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","换算油位和共事故油罐")
      } catch {
        case ex: Exception => MysqlDml.insert2("OilLevelConvert", "", "", "换算油位和共事故油罐", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
          ex.printStackTrace()
      }
    }

    //母线电压
    if (time(0) <= time(1) && (args(3) == "6" || args(3) == "all2")) {
      log.info("BusVoltage begining")
      var opt_start_time = sdfs.format(new Date)
      //MysqlDml.insert("BusVoltage",opt_start_time,opt_start_time,"begin",time(0),time(1))
      try {
        BusVoltageQualification.calcBusVoltage(ss, sc, hTable, time, step)
        var opt_end_time = sdfs.format(new Date)
        //MysqlDml.insert("BusVoltage",opt_start_time,opt_end_time,"end",time(0),time(1))
        MysqlDml.insert2("BusVoltage", "", "", "母线电压越限", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","母线电压")
      } catch {
        case ex: Exception => MysqlDml.insert2("BusVoltage", "", "", "母线电压越限", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
          ex.printStackTrace()
      }
    }

    //一次调频（新版，pmu高频数据，毫秒级）
    if (time(0) <= time(1) && (args(3) == "7")) {
      log.info("FirstFrequency2 begining")
      var opt_start_time = sdfs.format(new Date)
      try {
        FirstFrequency.calFrequency2(ss, sc, hTable, time, step)
        var opt_end_time = sdfs.format(new Date)
        MysqlDml.insert2("FirstFrequency2", "", "", "一次调频(pmu)", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","一次调频(pmu)")
      } catch {
        case ex: Exception => MysqlDml.insert2("FirstFrequency2", "", "", "一次调频(pmu)", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
          ex.printStackTrace()
      }
    }

    //一次调频(旧版，秒级)
    if (time(0) <= time(1) && (args(3) == "28")) {
      log.info("FirstFrequency begining")
      var opt_start_time = sdfs.format(new Date)
      try {
        FirstFrequency.calFrequency(ss, sc, hTable, time, step)
        var opt_end_time = sdfs.format(new Date)
        MysqlDml.insert2("FirstFrequency", "", "", "一次调频", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","一次调频")
      } catch {
        case ex: Exception => MysqlDml.insert2("FirstFrequency", "", "", "一次调频", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
          ex.printStackTrace()
      }
    }

    //日发电量考核
    if (time(0) <= time(1) && (args(3) == "9" || args(3) == "all2")) {
      log.info("DailyGenerationCheck begining")
      var opt_start_time = sdfs.format(new Date)
      //MysqlDml.insert("DailyGenerationCheck",opt_start_time,opt_start_time,"begin",time(0),time(1))
      try {
        DailyGenerationCheck.calcDayElectricQty(ss, sc, hTable, time, step)
        //DailyGenerationCheck.calcOilLevelConvert(ss,sc,hTable,time,step)
        var opt_end_time = sdfs.format(new Date)
        //MysqlDml.insert("DailyGenerationCheck",opt_start_time,opt_end_time,"end",time(0),time(1))
        MysqlDml.insert2("DailyGenerationCheck", "", "", "日发电量考核", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","日发电量考核")
      } catch {
        case ex: Exception => MysqlDml.insert2("DailyGenerationCheck", "", "", "日发电量考核", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
          ex.printStackTrace()
      }
    }

    //无功考核
    if (time(0) <= time(1) && (args(3) == "10" || args(3) == "all2")) {
      log.info("BusVoltage2 begining")
      var opt_start_time = sdfs.format(new Date)
      //MysqlDml.insert("BusVoltage2",opt_start_time,opt_start_time,"begin",time(0),time(1))
      try {
        BusVoltageQualification.calcReactivePower(ss, sc, hTable, time, step)
        //BusVoltageQualification.calcStartStop(ss,sc,hTable,time,step)
        var opt_end_time = sdfs.format(new Date)
        //MysqlDml.insert("BusVoltage2",opt_start_time,opt_end_time,"end",time(0),time(1))
        MysqlDml.insert2("BusVoltage2", "", "", "无功考核", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","无功考核")
      } catch {
        case ex: Exception => MysqlDml.insert2("BusVoltage2", "", "", "无功考核", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
          ex.printStackTrace()
      }
    }

    //增发电量考核
    if (time(0) <= time(1) && (args(3) == "11" || args(3) == "all")) {
      log.info("DailyGenerationCheck2 begining")
      var opt_start_time = sdfs.format(new Date)
      //MysqlDml.insert("DailyGenerationCheck2",opt_start_time,opt_start_time,"begin",time(0),time(1))
      try {
        DailyGenerationCheck.calcIncreasesElectricity(ss, sc, hTable, time, step)
        var opt_end_time = sdfs.format(new Date)
        //MysqlDml.insert("DailyGenerationCheck2",opt_start_time,opt_end_time,"end",time(0),time(1))
        MysqlDml.insert2("DailyGenerationCheck2", "", "", "增发电量考核", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","增发电量考核")
      } catch {
        case ex: Exception => MysqlDml.insert2("DailyGenerationCheck2", "", "", "增发电量考核", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
          ex.printStackTrace()
      }
    }

    //启停统计
    if (time(0) <= time(1) && (args(3) == "13" || args(3) == "all")) {
      log.info("StartStop2 begining")
      var opt_start_time = sdfs.format(new Date)
      //MysqlDml.insert("StartStop2",opt_start_time,opt_start_time,"begin",time(0),time(1))
      try {
        StartStop.calcStartStop(ss, sc, hTable, time, step)
        var opt_end_time = sdfs.format(new Date)
        //MysqlDml.insert("StartStop2",opt_start_time,opt_end_time,"end",time(0),time(1))
        MysqlDml.insert2("StartStop2", "", "", "启停统计", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","启停统计")
      } catch {
        case ex: Exception => MysqlDml.insert2("StartStop2", "", "", "启停统计", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
          ex.printStackTrace()
      }
    }

    //AGC调节
    if (time(0) <= time(1) && (args(3) == "14" || args(3) == "all2")) {
      log.info("AGC begining")
      var opt_start_time = sdfs.format(new Date)
      try {
        FirstFrequency.calAgc(ss, sc, hTable, time, step)
        var opt_end_time = sdfs.format(new Date)
        MysqlDml.insert2("AGC", "", "", "AGC调节", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","AGC调节")
      } catch {
        case ex: Exception => MysqlDml.insert2("AGC", "", "", "AGC调节", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
          ex.printStackTrace()
      }
    }

    //油温绕组碳刷打火评分
    if (time(0) <= time(1) && (args(3) == "18" || args(3) == "all2")) {
      log.info("Temperature begining")
      var opt_start_time = sdfs.format(new Date)

      //MysqlDml.insert("AGC",opt_start_time,opt_start_time,"begin",time(0),time(1))
      try {
        GroupPointMax.calCarbonBrush(ss, sc, hTable, time, step)
        GroupPointMax.calOilCoilTemperature(ss, sc, hTable, time, step)
        var opt_end_time = sdfs.format(new Date)
        //MysqlDml.insert("AGC",opt_start_time,opt_end_time,"end",time(0),time(1))
        MysqlDml.insert2("Temperature", "", "", "油温绕组碳刷打火评分", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","油温绕组碳刷打火评分")
      } catch {
        case ex: Exception => MysqlDml.insert2("Temperature", "", "", "油温绕组评分", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
          ex.printStackTrace()
      }
    }

    // 巡检程序在calcstat那边。
    //巡检告警 20210115跑失败了 先不跑||args(3)=="all2"
//    if (time(0) <= time(1) && (args(3) == "19")) {
//      log.info("MonitorAlert begining")
//      var opt_start_time = sdfs.format(new Date)
//
//      //MysqlDml.insert("AGC",opt_start_time,opt_start_time,"begin",time(0),time(1))
//      try {
//        MonitorAlert.calData(ss, sc, hTable, time, step)
//        var opt_end_time = sdfs.format(new Date)
//        //MysqlDml.insert("AGC",opt_start_time,opt_end_time,"end",time(0),time(1))
//        MysqlDml.insert2("MonitorAlert", "", "", "巡检告警", "SUCCESS", time(0), time(1), 0, "", opt_start_time, opt_end_time)
//      } catch {
//        case ex: Exception => MysqlDml.insert2("MonitorAlert", "", "", "巡检告警", "ERROR", time(0), time(1), 0, "", opt_start_time, sdfs.format(new Date))
//      }
//    }

    //红外测温
    if (time(0) <= time(1) && (args(3) == "20" || args(3) == "all")) {
      log.info("HONGWAI begining")
      var opt_start_time = sdfs.format(new Date)
      //MysqlDml.insert("StartStop2",opt_start_time,opt_start_time,"begin",time(0),time(1))
      try {
        GroupPointMax.calInfraredTemperature(ss, sc, hTable, time, step)
        var opt_end_time = sdfs.format(new Date)
        //MysqlDml.insert("StartStop2",opt_start_time,opt_end_time,"end",time(0),time(1))
        MysqlDml.insert2("HONGWAI", "", "", "红外测温", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","红外测温")
      } catch {
        case ex: Exception => MysqlDml.insert2("HONGWAI", "", "", "红外测温", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
          ex.printStackTrace()
      }
    }

    //协联曲线
    if (time(0) <= time(1) && (args(3) == "21" || args(3) == "all2")) {
      log.info("UnitPower2 begining")
      var opt_start_time = sdfs.format(new Date)
      try {
        UnitPower.calPowerCurve(ss, sc, hTable, time, step)
        var opt_end_time = sdfs.format(new Date)
        MysqlDml.insert2("UnitPower2", "", "", "协联曲线", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","协联曲线")
      } catch {
        case ex: Exception => MysqlDml.insert2("UnitPower2", "", "", "协联曲线", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
          ex.printStackTrace()
      }
    }

    //智能抄表日计算
    // （一天跑一次，一次算一天的数据）
    if (time(0) <= time(1) && (args(3) == "16")) {
      log.info("IntelligentMeter begining")
      var opt_start_time = sdfs.format(new Date)
      try {
        val sdf = new SimpleDateFormat("yyyy/MM/dd")
        var startTimeStamp = sdf.parse(time(0)).getTime()
        val endTimeStamp = sdf.parse(time(1)).getTime()

        while (startTimeStamp <= endTimeStamp) {
          val startTime = sdf.format(new Date(startTimeStamp))
          val time3 = Array(startTime, startTime)
          startTimeStamp = startTimeStamp + 3600 * 24 * 1000
          // 计算天、月、年的数据
          IntelligentMeter.calculateData_day(ss, sc, hTable, time3, step)
          IntelligentMeter.calculateData_mth(ss, sc, hTable, time3, step)
          IntelligentMeter.calculateData_year(ss, sc, hTable, time3, step)
        }

        var opt_end_time = sdfs.format(new Date)
        MysqlDml.insert2("IntelligentMeter", "", "", "智能抄表日计算", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","智能抄表日计算")
      } catch {
        case ex: Exception => MysqlDml.insert2("IntelligentMeter", "", "", "智能抄表日计算", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
          ex.printStackTrace()
      }
    }

    //组合测点最大平均
    if (time(0) <= time(1) && (args(3) == "17" || args(3) == "all2")) {
      log.info("GroupPointMax begining")
      val jdbcDF2 = ss.read
        .format("jdbc")
        .option("url", MysqlDml.url2)
        .option("dbtable", "hms_cm_scene_point")
        .option("user", MysqlDml.user2)
        .option("password", MysqlDml.password2)
        .option("driver", MysqlDml.driver2)
        .load()

      jdbcDF2.createOrReplaceTempView("t_item")
      //"item_no in ('B.1-2.3.5.1-1','B.1-1.4.4.1-1','B.1-1.3.5.1-1','B.1-1.5.2.1-1','B.1-1.3.4.1-1','B.1-2.3.4.1-1') "
      val itemId: Array[String] = ss.sql("select parent_assetid assetid from t_item where item_id in ('0a4df666f3464d3eadc09270c593465d', '1abf82cf82b546259d67c856c426a9a5', '2f0e2cdfcc8d46bc97ba3d0987f50503', '521aa9895b0440678e5fe56ccfa55a1e', '6060af71b6b545a7b2694bec59c25457', 'f730d396cfb049429d2a29a7d26a8196','3677994445b1485d90f293871499e2db'," +
        " '389c10647d334017a922d5d351d8ced7','949b786c3d2848aeb52933bcd38c5daa','d5d03478afdf4e88b33f4f982aa858f6','dabbdb77d02543cb93a0bdc6e96973b1','ddd6f53d4e874926a65ff91ed51abe82')").map(x => x.getString(0)).dropDuplicates().collect()
      log.info(itemId.mkString("#"))
      for (item <- itemId) {
        if (args(4) == "all" || args(4) == item) {
          var opt_start_time = sdfs.format(new Date)

          try {
            GroupPointMax.calCompPointStat(ss, sc, hTable, time, step, item)
            var opt_end_time = sdfs.format(new Date)
            MysqlDml.insert2("GroupPointMax" + item, "", "", "组合测点最大平均", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
            log.info("任务：{}计算成功！","组合测点最大平均")
          } catch {
            case ex: Exception => MysqlDml.insert2("GroupPointMax" + item, "", "", "组合测点最大平均", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
              ex.printStackTrace()
          }
        }
      }
    }

    // 公式计算在calcstat那边。
    //公式计算测点
//    if (time(0) <= time(1) && (args(3) == "22" || args(3) == "all3")) {
//      log.info("insertFormula2H begining")
//      var opt_start_time = sdfs.format(new Date)
//      try {
//        DftoMap.insertFormula2Hbase(ss, sc, time)
//        //DftoMap.calDataHSYW(ss,sc,hTable,time,step)
//        //DftoMap.insertFormula2H(ss,sc,time)
//        var opt_end_time = sdfs.format(new Date)
//        MysqlDml.insert2("insertFormula2H", "", "", "公式计算测点", "SUCCESS", time(0), time(1), 0, "", opt_start_time, opt_end_time)
//      } catch {
//        case ex: Exception => MysqlDml.insert2("insertFormula2H", "", "", "公式计算测点", "ERROR", time(0), time(1), 0, "", opt_start_time, sdfs.format(new Date))
//      }
//    }

    // (lyl)报表：日漏油量平均最大
    if (time(0) <= time(1) && (args(3) == "12")) {
      log.info("OilLevelConvert2 begining")
      var opt_start_time = sdfs.format(new Date)
      try {
        OilLevelConvert.calcOilStat(ss, sc, hTable, time, step)
        var opt_end_time = sdfs.format(new Date)
        MysqlDml.insert2("OilLevelConvert2", "", "", "日漏油量平均最大", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","日漏油量平均最大")
      } catch {
        case ex: Exception => MysqlDml.insert2("OilLevelConvert2", "", "", "日漏油量平均最大", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
          ex.printStackTrace()
      }
    }

    //hsyw换算油位，
    // 实时漏油量（8小时漏油量、24小时漏油量）
    // 每秒的值，保存到Nodejs服务，不存数据库
    if (time(0) <= time(1) && (args(3) == "24")) {
      log.info("OilLevelConvertReal begining")
      val opt_start_time = sdfs.format(new Date)
      try {
        OilLevelConvert.calcOilReal(ss, sc, hTable, time, step)
        val opt_end_time = sdfs.format(new Date)
        MysqlDml.insert2("OilLevelConvertReal", "", "", "实时漏油量", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","实时漏油量")
      } catch {
        case ex: Exception => MysqlDml.insert2("OilLevelConvertReal", "", "", "实时漏油量", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
          ex.printStackTrace()
      }
    }

    //日考核电量统计
    if (time(0) <= time(1) && (args(3) == "26")) {
      log.info("StatisticDay begining")
      var opt_start_time = sdfs.format(new Date)
      //MysqlDml.insert("TrfLoss",opt_start_time,opt_start_time,"begin",time3(0),time3(1))
      try {
        StatisticDay.calAgcDay(ss, sc, hTable, time, step)
        StatisticDay.calVoltageDay(ss, sc, hTable, time, step)
        var opt_end_time = sdfs.format(new Date)
        MysqlDml.insert2("StatisticDay", "", "", "日考核电量统计", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","日考核电量统计")
      } catch {
        case ex: Exception => MysqlDml.insert2("StatisticDay", "", "", "日考核电量统计", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
          ex.printStackTrace()
      }
    }

    // PMU开关量记录最大频率、最小频率、是否越界计算
    if (time(0) <= time(1) && (args(3) == "27")) {
      log.info("StatisticDay begining")
      val opt_start_time = sdfs.format(new Date)
      try {
        PmuSwitch.calcFreq(ss, sc)
        val opt_end_time = sdfs.format(new Date)
        MysqlDml.insert2("PmuKaiGuanFreq", "", "", "PMU开关量频率计算", "SUCCESS", time(0), time(1), 0, "任务执行成功", opt_start_time, opt_end_time)
        log.info("任务：{}计算成功！","PMU开关量频率计算")
      } catch {
        case ex: Exception => MysqlDml.insert2("PmuKaiGuanFreq", "", "", "PMU开关量频率计算", "ERROR", time(0), time(1), 0, ex.getMessage, opt_start_time, sdfs.format(new Date))
        ex.printStackTrace()
      }
    }

    log.info("ALL end")
    DruidUtils.newInstance().close();
    ss.stop()
  }

}

case class ColumnType(checkPoint: String, createTime: String, pvalue: Float)

case class ColumnType2(checkPoint: String, createTime: String, pvalue: String)

case class ColumnType3(checkPoint: String, createTime: String, pvalue: Double)

case class TimeType(today: String)

case class Data(name: String, time: String, value: Float) {}